using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class AddMilkTreatment : Telerik.WinControls.UI.RadForm
    {
        B_Building B_building = new B_Building();
        B_Mix B_mix = new B_Mix();
        B_TreatmentType B_treatmentType = new B_TreatmentType();
        B_MilkTreatment B_milktreatment = new B_MilkTreatment();
        public AddMilkTreatment()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        //load building for milking
        private void loadBuilding()
        {
            DataTable aTable = B_building.GetAllBuilding();
            drdBuilding.DataSource = aTable;
            drdBuilding.DisplayMember = "BuildingName";
            drdBuilding.ValueMember = "BuildingID";
            drdBuilding.Text = "(Select a location)";
        }

        //load mix list
        private void loadMixList()
        {
            DataTable aTable = B_mix.GetAllMix();
            grvMixList.DataSource = aTable;
        }

        //load treatment type
        private void loadTreatmentType()
        {
            DataTable aTable = B_treatmentType.GetAllTreatmentType();
            drdTreatmentType.DataSource = aTable;
            drdTreatmentType.DisplayMember = "TreatmentTypeName";
            drdTreatmentType.ValueMember = "TreatmentTypeID";
            drdTreatmentType.Text = "(Select a treatment type)";
        }
        private void AddMilkTreatment_Load(object sender, EventArgs e)
        {
            loadBuilding();
            loadMixList();
            loadTreatmentType();
        }

        private void btnAddTreatment_Click(object sender, EventArgs e)
        {
            if (grvMixList.SelectedRows.Count > 0)
            {
                //get the value from the form
                GridViewRowInfo row = grvMixList.SelectedRows[0];
                int mixID = int.Parse(row.Cells["MixID"].Value.ToString());
                int treatmentTypeID = int.Parse(drdTreatmentType.SelectedValue.ToString());
                DateTime time = dtpTimeMix.Value;
                int buildingID = int.Parse(drdBuilding.SelectedValue.ToString());
                string description = txtDescription.Text;

                MilkTreatment_Info milkTreatment = new MilkTreatment_Info();
                milkTreatment.Description = description;
                milkTreatment.MixtID = mixID;
                milkTreatment.Time = time;
                milkTreatment.TreatmentLocationID = buildingID;
                milkTreatment.TreatmentTypeID = treatmentTypeID;

                //insert data into the database
                bool isOk = B_milktreatment.CreateOneMilkTreatment(milkTreatment);
                if (isOk)
                {
                    MessageBox.Show(this, "Milk treatment added successful", "Milk treatment", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    resetFields();
                }
            }
            else
            {
                MessageBox.Show(this, "You must least one livestock", "Mix treatment", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void resetFields()
        {
            txtDescription.Text = "";
            txtDescription.Focus();
        }

        private void grvMixList_Click(object sender, EventArgs e)
        {
            if (grvMixList.SelectedRows.Count > 0)
            {
                GridViewRowInfo row = grvMixList.SelectedRows[0];
                int mixID = int.Parse(row.Cells["MixID"].Value.ToString());
                DataTable aTable = B_milktreatment.GetAllMilkTreatment();
                bool isExist = false;
                for (int i = 0; i < aTable.Rows.Count; i++)
                {
                    int aMix = int.Parse(aTable.Rows[i]["MixtID"].ToString());
                    if (mixID == aMix)
                        isExist = true;
                }
                if (isExist)
                {
                    setEnableControl(false);
                }
                else
                {
                    setEnableControl(true);
                }
            }
        }

        //set enable control
        private void setEnableControl(bool enable)
        {
            drdBuilding.Enabled = enable;
            drdTreatmentType.Enabled = enable;
            dtpTimeMix.Enabled = enable;
            btnAddTreatment.Enabled = enable;
            txtDescription.Enabled = enable;
        }
    }
}
